<?php

/**
 * Owner property
 *
 * The owner property is an href property.
 *
   * Copyright © 2010 Jack Cleaver
   * 
   * This plugin provides functionality added by CalDAV (RFC 4791)
   * It implements new reports, and the MKCALENDAR method.
   *
   * This file is part of Slash.
   *
   * Slash is free software: you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published by
   * the Free Software Foundation, either version 3 of the License, or
   * (at your option) any later version.
   *
   * Slash is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
   *
   * You should have received a copy of the GNU General Public License
   * along with Slash.  If not, see <http://www.gnu.org/licenses/>.
 */
class Slash_ACL_Property_Owner 
	extends Sabre_DAV_Property
	implements Sabre_DAV_Property_IHref
{

    private $href;

    /**
     * Creates the property.
     *
     * The 'type' argument must be one of the type constants defined in this class.
     *
     * 'href' is only required for the HREF type.
     * 
     * @param int $type 
     * @param string $href 
     * @return void
     */
    public function __construct($href) 
    {
      $this->href = $href;
    }


    /**
     * Returns the principal uri. 
     * 
     * @return string
     */
    public function getHref() 
    {
    	if ($this->href instanceof Slash_ACL_Property_Href)
    	{
        return $this->href->getHref();
    	} else {
    		return $this->href;
    	}
    }

  static function unserialize(DOMElement $ownerElement) 
	{
    $nlHrefs = $ownerElement->getElementsByTagNameNS("urn:DAV", "href");
    if ($nlHrefs->length > 0)
    {
      return new self($ownerElement->textContent);
    } else {
      $hrefElement = $nlHrefs->item(0);
      return new self(Slash_ACL_Property_Href::unserialize($hrefElement));
    }
  }

	public function serialize(Sabre_DAV_Server $server, DOMElement $owner) 
  {
    $prefix = $server->xmlNamespaces['DAV:'];
    $doc = $owner->ownerDocument;
    if ($this->href instanceof Slash_ACL_Property_Href)
    {
      $href = $doc->createElement("$prefix:href");
      $this->href->serialize($server, $href);
      $owner->appendChild($href);
    } else {
      $owner->appendChild(new DOMText($this->href));
    }
  }
}
